//
//  HGDiscern.m
//  HGMM
//
//  Created by huan liu on 2019/1/23.
//  Copyright © 2019年 xwx. All rights reserved.
//

#import "HGDiscern.h"
#import <AipOcrSdk/AipOcrSdk.h>
#import "UIView+ViewController.h"
#import "NSDictionary+json.h"
//#import "DiscernViewController.h"
#import "LYYNavigationController.h"


@implementation HGDiscern

//扫描护照
-(void)discernPassport: (CDVInvokedUrlCommand *)command{
    

     CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"iOS不支持护照识别"];
     [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    
     /*
    DiscernViewController *dis = [[DiscernViewController alloc]init];
    LYYNavigationController *lyy = [[LYYNavigationController alloc]initWithRootViewController:dis];
    [self.webView.viewContoller presentViewController:lyy animated:YES completion:nil];
    */

}

//扫描身份证
-(void)discernIDCard: (CDVInvokedUrlCommand *)command
{
    if (command.arguments.count !=1 ) {
      CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"args is wrong!"];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        return ;
    }
    
    NSInteger p = [command.arguments[0] integerValue];
    
    //正面
    if (p == 1)
    {
        [self idcardOCROnlineFront:command];
    }
    else if (p == 2) //反面
    {
        [self idcardOCROnlineBack:command];
        
    }
   
}



- (void)idcardOCROnlineFront :(CDVInvokedUrlCommand *)command{
    
    
    
    UIViewController * vc =
    [AipCaptureCardVC ViewControllerWithCardType:CardTypeIdCardFont
                                 andImageHandler:^(UIImage *image) {
                                     
                                     
                                     [[AipOcrService shardService] detectIdCardFrontFromImage:image withOptions:nil successHandler:^(id result) {
                                         
                                         NSDictionary *resultDic = (NSDictionary *)result;
                                         NSDictionary *words_result = [resultDic objectForKey:@"words_result"];
                                         NSString *name = [[words_result objectForKey:@"姓名"] objectForKey:@"words"];
                                         NSString *birthday = [[words_result objectForKey:@"出生"] objectForKey:@"words"];
                                         NSString *idNumber = [[words_result objectForKey:@"公民身份号码"] objectForKey:@"words"];
                                         NSString *gender = [[words_result objectForKey:@"性别"] objectForKey:@"words"];
                                        NSString *address = [[words_result objectForKey:@"住址"] objectForKey:@"words"];
                                         NSString *ethnic = [[words_result objectForKey:@"民族"] objectForKey:@"words"];
                                         
                                         NSDictionary *resultDIC1 = @{
                                                                      @"ethnic":ethnic,
                                                                      @"name":name,
                                                                      @"birthday":birthday,
                                                                      @"idNumber":idNumber,
                                                                      @"gender":gender,
                                                                      @"address":address
                                                                      
                                                                      };

                                         
                                         NSString *resultString = [resultDIC1 toReadableJSONString];
                                            
                                             
                                             CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:resultString];
                                             [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
                                        
                                         
                                         [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                            
                                             [self.webView.viewContoller dismissViewControllerAnimated:YES completion:nil];
                                         }];
                                         
                                         
                                     } failHandler:^(NSError *err) {
                                         
                                        // [self.webView.viewContoller dismissViewControllerAnimated:YES completion:^{
                                             
                                             CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"识别失败"];
                                             [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
                                     //    }];
                                         
                                       // [self.webView.viewContoller dismissViewControllerAnimated:YES completion:nil];
                                         
                                         [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                             
                                             [self.webView.viewContoller dismissViewControllerAnimated:YES completion:nil];
                                         }];
                                         
                                     }];
                                     
                                     
                                 }];
    
    [self.webView.viewContoller presentViewController:vc animated:YES completion:nil];
    
}


- (void)idcardOCROnlineBack:(CDVInvokedUrlCommand *)command{
    
    UIViewController * vc =
    [AipCaptureCardVC ViewControllerWithCardType:CardTypeIdCardBack
                                 andImageHandler:^(UIImage *image) {
  
                                     
                                     [[AipOcrService shardService] detectIdCardBackFromImage:image
                                                                                     withOptions:nil
                                                                                  successHandler:^(id result) {
                                         
                NSDictionary *resultDic = (NSDictionary *)result;
                                                                                      
                                                                                 
                  
                  NSDictionary *words_result = [resultDic objectForKey:@"words_result"];
                  NSString *issueAuthority = [[words_result objectForKey:@"签发机关"] objectForKey:@"words"];
                  NSString *expiryDate = [[words_result objectForKey:@"失效日期"] objectForKey:@"words"];
                  NSString *signDate = [[words_result objectForKey:@"签发日期"] objectForKey:@"words"];
                 
                  
                  NSDictionary *resultDIC1 = @{
                                               @"issueAuthority":issueAuthority,
                                               @"expiryDate":expiryDate,
                                               @"signDate":signDate
                                            
                                               };
                  
                  
                                                                                      
                                                                                      
                        NSString *resultString = [resultDIC1 toReadableJSONString];
                                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:resultString];
                                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
                                                                                      
                                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                            
                                            [self.webView.viewContoller dismissViewControllerAnimated:YES completion:nil];
                                        }];
                                         
                                     } failHandler:^(NSError *err) {
                                         
                                         CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"识别失败"];
                                         [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
                                         [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                             
                                             [self.webView.viewContoller dismissViewControllerAnimated:YES completion:nil];
                                         }];
                                         
                                     }];
                                    
                                     
                                 }];
    [self.webView.viewContoller presentViewController:vc animated:YES completion:nil];
}


@end
